package com.htht.job.executor.activemq;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;

import com.alibaba.fastjson.JSON;
import com.htht.job.core.enums.MonitorQueue;
import com.htht.job.executor.model.registry.Registry;
import com.htht.job.executor.redis.RedisService;
import com.htht.job.executor.service.registry.RegistryService;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jms.JmsException;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Component;

import com.htht.job.core.biz.model.ReturnT;
import com.htht.job.core.biz.model.TriggerParam;

@Component
public class JMSProducer {
    @Autowired
    private JmsTemplate jmsTemplate;
    @Autowired
    private RedisService redisService;
    @Autowired
    private RegistryService registryService;

    @Value("${mqDestination}")
    String mqDestination;
    public  ReturnT<String>  sendMessage( TriggerParam triggerParam) {
        try {
            Registry registry=registryService.findByMqDestination(mqDestination);
            String key=registry.getRegistryKey()+":"+registry.getRegistryIp()+registry.getPort();
            //存储正在排队的job
            redisService.lPush(MonitorQueue.NODE_LINE_QUEUE+key, triggerParam.getJobId());
            Destination destination = new ActiveMQQueue(mqDestination);
			this.jmsTemplate.convertAndSend(destination,triggerParam);
		} catch (Exception e) {
			e.printStackTrace();
			return  ReturnT.FAIL;
		}  
        return  ReturnT.SUCCESS;
    }
}